/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStreamWriter;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.InetAddress;
/*     */ import java.net.Socket;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.regex.Pattern;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetLoggerStats
/*     */   extends AdminDocumentHandler
/*     */ {
/* 105 */   private static final String ZMRRDFETCH = LC.zimbra_home.value() + "/libexec/zmrrdfetch";
/* 106 */   private static final Pattern SPLIT_PATTERN = Pattern.compile("\\\"?\\s*,\\s*\\\"?");
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/* 110 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 115 */     Provisioning prov = Provisioning.getInstance();
/* 116 */     String monitorHost = prov.getConfig().getAttr("zimbraLogHostname");
/* 117 */     if ((monitorHost == null) || (monitorHost.trim().equals("")))
/* 118 */       throw ServiceException.FAILURE("zimbraLogHostname is not configured", null);
/* 119 */     Server monitorServer = prov.get(Key.ServerBy.name, monitorHost);
/*     */     
/* 121 */     checkRight(zsc, context, monitorServer, Rights.Admin.R_getServerStats);
/*     */     
/* 123 */     if (monitorServer == null)
/* 124 */       throw ServiceException.FAILURE("could not find zimbraLogHostname server: " + monitorServer, null);
/* 125 */     if (!prov.getLocalServer().getId().equalsIgnoreCase(monitorServer.getId())) {
/* 126 */       return proxyRequest(request, context, monitorServer);
/*     */     }
/* 128 */     Element response = zsc.createElement(AdminConstants.GET_LOGGER_STATS_RESPONSE);
/* 129 */     boolean loggerEnabled = false;
/* 130 */     Server local = prov.getLocalServer();
/* 131 */     String[] services = local.getMultiAttr("zimbraServiceEnabled");
/* 132 */     if (services != null) {
/* 133 */       for (int i = 0; (i < services.length) && (!loggerEnabled); i++) {
/* 134 */         loggerEnabled = "logger".equals(services[i]);
/*     */       }
/*     */     }
/* 137 */     if (loggerEnabled) {
/* 138 */       Element host = request.getOptionalElement("hostname");
/* 139 */       Element stats = request.getOptionalElement("stats");
/* 140 */       Element start = request.getOptionalElement("startTime");
/* 141 */       Element end = request.getOptionalElement("endTime");
/* 142 */       Element values = null;
/* 143 */       HashSet<String> counters = null;
/* 144 */       if (stats != null) {
/* 145 */         values = stats.getOptionalElement("values");
/* 146 */         if (values != null) {
/* 147 */           List<Element> counterList = values.listElements("stat");
/* 148 */           if (counterList.size() > 0)
/* 149 */             counters = new HashSet(counterList.size());
/* 150 */           for (Element e : counterList) {
/* 151 */             counters.add(e.getAttribute("name"));
/*     */           }
/*     */         }
/*     */       }
/*     */       
/* 156 */       if ((host == null) && (stats == null))
/*     */       {
/* 158 */         fetchHostnames(response);
/* 159 */       } else if ((host != null) && (stats == null))
/*     */       {
/* 161 */         fetchGroupNames(response, host.getAttribute("hn"));
/* 162 */       } else if ((stats != null) && (host == null)) {
/* 163 */         String limitStr = stats.getAttribute("limit", null);
/* 164 */         boolean limit = (limitStr != null) && (!"no".equalsIgnoreCase(limitStr)) && (!"false".equalsIgnoreCase(limitStr)) && (!"0".equalsIgnoreCase(limitStr));
/*     */         
/*     */ 
/*     */ 
/* 168 */         String startTime = null;
/* 169 */         String endTime = null;
/* 170 */         if ((start != null) || (end != null)) {
/* 171 */           if ((start == null) || (end == null)) {
/* 172 */             throw ServiceException.FAILURE("both start and end must be specified", null);
/*     */           }
/* 174 */           startTime = start.getAttribute("time");
/* 175 */           endTime = end.getAttribute("time");
/* 176 */           fetchColumnData(response, counters, stats.getAttribute("name"), startTime, endTime, limit);
/*     */         }
/*     */         else
/*     */         {
/* 180 */           fetchColumnData(response, counters, stats.getAttribute("name"), limit);
/*     */         }
/*     */       }
/* 183 */       else if ((stats != null) && (host != null))
/*     */       {
/* 185 */         String statsText = stats.getText();
/* 186 */         if ((statsText != null) && (statsText.trim().length() > 0))
/*     */         {
/* 188 */           fetchColumnNames(response, host.getAttribute("hn"), stats.getAttribute("name"));
/*     */ 
/*     */         }
/*     */         else
/*     */         {
/* 193 */           String limitStr = stats.getAttribute("limit", null);
/* 194 */           boolean limit = (limitStr != null) && (!"no".equalsIgnoreCase(limitStr)) && (!"false".equalsIgnoreCase(limitStr)) && (!"0".equalsIgnoreCase(limitStr));
/*     */           
/*     */ 
/*     */ 
/* 198 */           String startTime = null;
/* 199 */           String endTime = null;
/*     */           
/* 201 */           if ((start != null) || (end != null)) {
/* 202 */             if ((start == null) || (end == null)) {
/* 203 */               throw ServiceException.FAILURE("both start and end must be specified", null);
/*     */             }
/* 205 */             startTime = start.getAttribute("time");
/* 206 */             endTime = end.getAttribute("time");
/* 207 */             fetchColumnData(response, counters, host.getAttribute("hn"), stats.getAttribute("name"), startTime, endTime, limit);
/*     */ 
/*     */           }
/*     */           else
/*     */           {
/* 212 */             fetchColumnData(response, counters, host.getAttribute("hn"), stats.getAttribute("name"), limit);
/*     */           }
/*     */         }
/*     */       }
/*     */       else
/*     */       {
/* 218 */         throw ServiceException.FAILURE("Unknown query combination", null);
/*     */       }
/*     */     } else {
/* 221 */       response.addElement("note").setText("Logger is not enabled");
/*     */     }
/*     */     
/* 224 */     return response;
/*     */   }
/*     */   
/*     */   static void fetchHostnames(Element response) throws ServiceException {
/* 228 */     Iterator<String> results = execfetch(new String[] { "-n" });
/*     */     
/* 230 */     while (results.hasNext()) {
/* 231 */       Element host = response.addElement("hostname");
/* 232 */       host.addAttribute("hn", (String)results.next());
/*     */     }
/*     */   }
/*     */   
/* 236 */   static void fetchGroupNames(Element response, String hostname) throws ServiceException { Iterator<String> results = execfetch(new String[] { "-l", "-h", hostname });
/* 237 */     Element host = response.addElement("hostname");
/* 238 */     host.addAttribute("hn", hostname);
/* 239 */     while (results.hasNext()) {
/* 240 */       Element stats = host.addElement("stats");
/* 241 */       stats.addAttribute("name", (String)results.next());
/*     */     }
/*     */   }
/*     */   
/* 245 */   static void fetchColumnNames(Element response, String hostname, String group) throws ServiceException { Iterator<String> results = execfetch(new String[] { "-l", "-h", hostname, "-f", group });
/* 246 */     Element host = response.addElement("hostname");
/* 247 */     host.addAttribute("hn", hostname);
/* 248 */     Element stats = host.addElement("stats");
/* 249 */     stats.addAttribute("name", group);
/* 250 */     Element values = stats.addElement("values");
/* 251 */     while (results.hasNext()) {
/* 252 */       String result = (String)results.next();
/* 253 */       String name = result;
/* 254 */       int idx = result.indexOf(" :: ");
/* 255 */       Element stat = values.addElement("stat");
/* 256 */       if (idx != -1) {
/* 257 */         name = result.substring(0, idx);
/* 258 */         String unit = result.substring(idx + 4);
/* 259 */         stat.addAttribute("type", unit);
/*     */       }
/* 261 */       stat.addAttribute("name", name);
/*     */     }
/*     */   }
/*     */   
/*     */   static void fetchColumnData(Element response, Set<String> counters, String group, String start, String end, boolean limit) throws ServiceException {
/*     */     Iterator<String> results;
/*     */     Iterator<String> results;
/* 268 */     if (limit) {
/* 269 */       results = execfetch(new String[] { "-c", "-f", group, "-s", start, "-e", end });
/*     */     } else
/* 271 */       results = execfetch(new String[] { "-f", group, "-s", start, "-e", end });
/* 272 */     populateResponseData(response, counters, group, results);
/*     */   }
/*     */   
/*     */   static void fetchColumnData(Element response, Set<String> counters, String hostname, String group, String start, String end, boolean limit) throws ServiceException {
/*     */     Iterator<String> results;
/*     */     Iterator<String> results;
/* 278 */     if (limit) {
/* 279 */       results = execfetch(new String[] { "-c", "-h", hostname, "-f", group, "-s", start, "-e", end });
/*     */     }
/*     */     else {
/* 282 */       results = execfetch(new String[] { "-h", hostname, "-f", group, "-s", start, "-e", end });
/*     */     }
/*     */     
/* 285 */     populateResponseData(response, counters, hostname, group, results);
/*     */   }
/*     */   
/*     */   static void fetchColumnData(Element response, Set<String> counters, String group, boolean limit) throws ServiceException { Iterator<String> results;
/*     */     Iterator<String> results;
/* 290 */     if (limit) {
/* 291 */       results = execfetch(new String[] { "-c", "-f", group });
/*     */     } else
/* 293 */       results = execfetch(new String[] { "-f", group });
/* 294 */     populateResponseData(response, counters, group, results);
/*     */   }
/*     */   
/*     */   static void fetchColumnData(Element response, Set<String> counters, String hostname, String group, boolean limit) throws ServiceException { Iterator<String> results;
/*     */     Iterator<String> results;
/* 299 */     if (limit) {
/* 300 */       results = execfetch(new String[] { "-c", "-h", hostname, "-f", group });
/*     */     } else
/* 302 */       results = execfetch(new String[] { "-h", hostname, "-f", group });
/* 303 */     populateResponseData(response, counters, hostname, group, results);
/*     */   }
/*     */   
/*     */   static void populateResponseData(Element response, Set<String> counters, String hostname, String group, Iterator<String> results)
/*     */     throws ServiceException
/*     */   {
/* 309 */     if (!results.hasNext()) {
/* 310 */       return;
/*     */     }
/* 312 */     String line = (String)results.next();
/* 313 */     String[] columns = SPLIT_PATTERN.split(line);
/*     */     
/* 315 */     Element host = response.addElement("hostname");
/* 316 */     host.addAttribute("hn", hostname);
/* 317 */     Element stats = host.addElement("stats");
/* 318 */     stats.addAttribute("name", group);
/* 319 */     while (results.hasNext()) {
/* 320 */       line = (String)results.next();
/* 321 */       String[] data = SPLIT_PATTERN.split(line);
/*     */       
/* 323 */       boolean rowHasData = false;
/* 324 */       int i = 1; for (int j = data.length; i < j; i++)
/* 325 */         rowHasData = (rowHasData) || ((data[i] != null) && (!data[i].trim().equals("")));
/* 326 */       if (rowHasData) {
/* 327 */         Element values = stats.addElement("values");
/* 328 */         values.addAttribute("t", data[0]);
/* 329 */         int i = 1; for (int j = data.length; i < j; i++) {
/* 330 */           if ((counters == null) || (counters.size() <= 0) || (counters.contains(columns[i])))
/*     */           {
/* 332 */             Element stat = values.addElement("stat");
/* 333 */             stat.addAttribute("name", columns[i]);
/* 334 */             if (data[i] != null)
/* 335 */               stat.addAttribute("value", data[i]);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   static void populateResponseData(Element response, Set<String> counters, String group, Iterator<String> results) throws ServiceException {
/* 343 */     Element host = null;
/* 344 */     Element stats = null;
/* 345 */     String[] columns = null;
/*     */     
/*     */ 
/* 348 */     boolean needColumns = false;
/* 349 */     while (results.hasNext()) {
/* 350 */       String line = (String)results.next();
/* 351 */       if (!"".equals(line.trim()))
/* 352 */         if (line.startsWith("Host: ")) {
/* 353 */           String hostname = line.substring(line.indexOf(" ") + 1);
/*     */           
/* 355 */           host = response.addElement("hostname");
/* 356 */           host.addAttribute("hn", hostname);
/*     */           
/* 358 */           stats = host.addElement("stats");
/* 359 */           stats.addAttribute("name", group);
/*     */           
/* 361 */           needColumns = true;
/*     */ 
/*     */ 
/*     */         }
/* 365 */         else if (needColumns) {
/* 366 */           columns = SPLIT_PATTERN.split(line);
/* 367 */           needColumns = false;
/*     */         }
/*     */         else
/*     */         {
/* 371 */           String[] data = SPLIT_PATTERN.split(line);
/*     */           
/* 373 */           boolean rowHasData = false;
/* 374 */           int i = 1; for (int j = data.length; i < j; i++)
/* 375 */             rowHasData = (rowHasData) || ((data[i] != null) && (!data[i].trim().equals("")));
/* 376 */           if (rowHasData) {
/* 377 */             Element values = stats.addElement("values");
/* 378 */             values.addAttribute("t", data[0]);
/* 379 */             int i = 1; for (int j = data.length; i < j; i++)
/* 380 */               if ((counters == null) || (counters.size() <= 0) || (counters.contains(columns[i])))
/*     */               {
/* 382 */                 Element stat = values.addElement("stat");
/* 383 */                 stat.addAttribute("name", columns[i]);
/* 384 */                 if (data[i] != null)
/* 385 */                   stat.addAttribute("value", data[i]);
/*     */               }
/*     */           }
/*     */         }
/*     */     }
/*     */   }
/*     */   
/*     */   static Iterator<String> execfetch(String... args) throws ServiceException {
/* 393 */     BufferedReader in = null;
/*     */     try {
/* 395 */       final Socket socket = new Socket(InetAddress.getByName(null), LC.logger_zmrrdfetch_port.intValue());
/* 396 */       final PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"));
/*     */       
/* 398 */       StringBuilder cmdline = new StringBuilder();
/* 399 */       for (String arg : args) {
/* 400 */         cmdline.append(arg).append(" ");
/*     */       }
/* 402 */       cmdline.setLength(cmdline.length() - 1);
/* 403 */       out.println(cmdline.toString());
/* 404 */       out.flush();
/* 405 */       in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/* 406 */       BufferedReader inbr = in;
/*     */       
/*     */ 
/* 409 */       new Iterator() {
/*     */         String line;
/* 411 */         boolean isClosed = false;
/*     */         
/*     */         public boolean hasNext() {
/*     */           try {
/* 415 */             this.line = this.val$inbr.readLine();
/*     */           }
/*     */           catch (IOException e) {
/* 418 */             ZimbraLog.soap.error("GetLoggerStats IOE", e);
/* 419 */             this.line = null;
/*     */           }
/*     */           
/* 422 */           if (this.line == null) {
/*     */             try {
/* 424 */               out.close();
/* 425 */               this.val$inbr.close();
/* 426 */               socket.close();
/* 427 */               this.isClosed = true;
/*     */             }
/*     */             catch (IOException e) {}
/*     */           }
/* 431 */           return this.line != null;
/*     */         }
/*     */         
/*     */         public String next() {
/* 435 */           if ((!this.isClosed) && (this.line == null)) {
/*     */             try {
/* 437 */               this.val$inbr.close();
/*     */             }
/*     */             catch (IOException e) {}
/* 440 */             throw new IllegalStateException("hasNext not called");
/*     */           }
/* 442 */           if (this.isClosed)
/* 443 */             throw new IllegalStateException("no more results");
/* 444 */           String l = this.line;
/* 445 */           this.line = null;
/* 446 */           return l;
/*     */         }
/*     */         
/* 449 */         public void remove() { throw new UnsupportedOperationException("remove"); }
/*     */       };
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 454 */       throw ServiceException.FAILURE("Unable to read logger stats", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 460 */     relatedRights.add(Rights.Admin.R_getServerStats);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/GetLoggerStats.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */